<?php
DEFINE('ID', 0);
DEFINE('NAME', 1);
DEFINE('SUBCATEGORY', 2);
DEFINE('CATEGORY', 3);
DEFINE('PRICE', 4);

require_once("scripts/cart_engine.php");

function displayProductOnList($productInfo)
{
	echo ('<div class="product">');
	echo ('<p>'.$productInfo[NAME].'</p>');
	echo ('<br><a href="index.php?content=product_details&id='.$productInfo[ID].'"><small>Pokaż szczegóły</small></a>');
	if (isset($_SESSION["moderator"]) && $_SESSION["moderator"] == 1) {
		echo ('<a href="index.php?content=edit_product&id='.$productInfo[ID].'"><small>Edytuj</small></a>');
		echo ('<a href="index.php?content=delete_product&id='.$productInfo[ID].'"><small>Usuń</small></a>');
	}
	echo ('<hr>');
	echo ('</div>');
}

function displayProductWithDetails($productInfo)
{
	echo ('<div class="details">');
	echo ('<h2>'.$productInfo[NAME].'</h2>');
	echo ('<p class="category">kategoria: '.$productInfo[CATEGORY].'</p>');
	echo ('<p class="subcategory">podkategoria: '.$productInfo[SUBCATEGORY].'</p>');
	echo ('<p class="price">cena: '.$productInfo[PRICE].' zł</p>');
	echo ('<form id="addtocart" name="addtocart" action="index.php?content=cart_hiding_place" method="POST">');
	echo ('<input name="action" type="hidden" value="addtocart">');
	echo ('<input name="id" type="hidden" value="'.$productInfo[ID].'">');
	echo ('<input name="submit" type="submit" value="Do koszyka">');
	echo ('</form>');
	echo ('<form id="addtohiding" name="addtohiding" action="index.php?content=cart_hiding_place" method="POST">');
	echo ('<input name="action" type="hidden" value="addtohiding">');
	echo ('<input name="id" type="hidden" value="'.$productInfo[ID].'">');
	echo ('<input name="submit" type="submit" value="Do przechowalni">');
	echo ('</form>');
	echo ('</div>');
}

function displayProductOnCart($productInfo)
{
	echo ('<div class="cart">');
	echo ('<p>'.$productInfo[NAME].'</p>');
	echo ('<a href="index.php?content=cart_hiding_place&action=removefromcart&id='.$productInfo[ID].'"><small>Usuń z koszyka</small></a> ');
	echo ('<a href="index.php?content=cart_hiding_place&action=fromcarttohiding&id='.$productInfo[ID].'"><small>Przenieś do przechowalni</small></a> ');
	echo ('</div>');
	echo ('<br>');
	
}

function displayProductInHidingPlace($productInfo)
{
	echo ('<div class="hiding">');
	echo ('<p>'.$productInfo[NAME].'</p>');
	echo ('<a href="index.php?content=cart_hiding_place&action=removefromhiding&id='.$productInfo[ID].'"><small>Usuń z przechowalni</small></a> ');
	echo ('<a href="index.php?content=cart_hiding_place&action=fromhidingtocart&id='.$productInfo[ID].'"><small>Przenieś do koszyka</small></a> ');
	echo ('</div>');
	echo ('<br>');
}

function getSpecifiedProductFromDatabase($connection, $id)
{
	$stmt = mysqli_prepare($connection, "SELECT * FROM produkty WHERE id = ?");
	mysqli_stmt_bind_param($stmt, 'd', $id);
	mysqli_stmt_execute($stmt);
	mysqli_stmt_bind_result($stmt, $product[ID], $product[NAME], $product[SUBCATEGORY], $product[CATEGORY], $product[PRICE]);
	mysqli_stmt_fetch($stmt);
	mysqli_stmt_close($stmt);
	return $product;
}

function deleteSpecifiedProductFromDatabase($connection, $id)
{
	if (isset($_SESSION["moderator"]) && $_SESSION["moderator"] == 1) {
		$stmt = mysqli_prepare($connection, "DELETE FROM produkty WHERE id = ?");
		mysqli_stmt_bind_param($stmt, 'd', $id);
		mysqli_stmt_execute($stmt);
		mysqli_stmt_close($stmt);
	}
}

?>